<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2018/8/21
 * Time: 14:54
 * 职称
 */
namespace app\admin\controller;
use think\Db;
class Positional  extends Base {
    public function index(){
        if(request()->isAjax()){
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::table('up_assortment')->alias('a')
                ->join('up_admin ua','ua.id = a.admin_uid','left')
                ->where('a.assortment_type',3)
                ->field('ua.username,a.*')
                ->count();
            $list = Db::table('up_assortment')->alias('a')
                ->join('up_admin ua','ua.id = a.admin_uid','left')
                ->where('a.assortment_type',3)
                ->field('ua.username,a.*')
                ->page($page, $limit)
                ->select();
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        return $this->fetch();
    }

    /**
     * 删除
     */
    public function batchDelBanner(){
        $ids = input('param.ids');
        Db::startTrans();// 启动事务
        $res = Db::table('up_assortment')->where('assortment_id',$ids)->delete();

        if($res){
            Db::commit();
            writelog(session('uid'),session('username'),'删除成功',1);
            return json(['code' => 200, 'data' => '', 'msg' => '删除成功']);
        }else{
            Db::rollback();
            writelog(session('uid'),session('username'),'删除失败',2);
            return json(['code' => 100, 'data' => '', 'msg' => '删除失败']);
        }
    }

    /***
     * @return mixed
     * 添加
     */
    public function add(){
        if(request()->isAjax()){
            $name = array_filter(explode(' ',input('param.title')));
            foreach ($name as $key=>$vo){
                $where['assortment_name'] = $vo;
                $where['assortment_type'] = 3;
                $assortment_id = Db::table('up_assortment')->where($where)->value('assortment_id');
                if($assortment_id){
                    return json(['code' => 100, 'data' => '', 'msg' => '不能重复添加职称名称']);
                }
                $a['assortment_name'] = $vo;
                $a['assortment_type'] = 3;
                $a['admin_uid'] = session('uid');
                $dataAll[] = $a;
            }
            $res = Db::table('up_assortment')->insertAll($dataAll);
            if($res){
                return json(['code' => 200, 'data' =>"", 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '添加失败']);
            }
        }

        return $this->fetch();
    }

    /***
     * 编辑
     */
    public function update(){
        if(request()->isAjax()){
            $name  = input('param.title');
            $id = input('param.id');
            $where['assortment_name'] = $name;
            $where['assortment_type'] = 3;
            $where['assortment_id'] = array('NEQ',$id);
            $assortment_id = Db::table('up_assortment')->where($where)->value('assortment_id');
            if($assortment_id){
                return json(['code' => 100, 'data' => '', 'msg' => '不能重复添加医院名称']);
            }
            $data['assortment_name'] = $name;
            $data['update_time'] = date('Y-m-m H:i:s',time());
            $res = Db::table('up_assortment')->where('assortment_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'data' =>"", 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '编辑失败']);
            }
        }
        $id = input('param.id');
        $list = Db::table('up_assortment')->where('assortment_id',$id)->find();
        $this->assign('list',$list);
        return $this->fetch();
    }
}